<template>
	<view class="container">
		<uni-nav-bar class="ch-nav" height="88rpx" backgroundColor="#ffffff" leftWidth="90rpx" rightWidth="90rpx" color="#333" :fixed="true" :padding="false" :border="false" @clickLeft="$goBack(1)">
			<template slot="left">
				<image class="left" src="@/public/images/public/dhl_fhjt.png" mode="aspectFit"></image>
			</template>
			<text class="title">我的留言</text>
		</uni-nav-bar>
		<view class="head flex-r ac" :class="navBorder ? 'navborder':''">
			<template v-for="(item,index) in tabList">
				<view class="tab-list flex-r ac" :class="{'active': tabCur == index}" :key="index" @tap="tabChange(index)">
					<image src="@/public/images/public/fgx.png" mode="aspectFill" v-if="index != 0"></image>
					<text class="text-regular">{{item.name}}</text>
				</view>
			</template>
		</view>
		<template v-for="(item,index) in tabList">
			<keep-alive>
				<view class="body bottom-ios" @touchstart="touchStart" @touchend="touchEnd" :key="index" v-if="tabCur == index">
					<EmptyContent v-if="!comments[tabCur][0]" top="250rpx" text="暂无留言" img="qsy_zwxl" @refresh="getUserComments"></EmptyContent>
					<view class="comments-list">
						<template v-for="(comment, index) in comments[tabCur]">
							<view class="comments-list-item flex-c" :key="index" v-if="!comment.isDelete">
								<view class="comment-content">
									<text class="reply-name" v-if="comment.replyUserName">回复{{comment.replyUserName}}：</text>
									<text>{{comment.content}}</text>
								</view>
								<uni-dateformat class="comment-time" :date="comment.createTime" format="yyyy.MM.dd hh:mm"></uni-dateformat>
								<view class="comment-title text-info" v-if="comment.title" @tap="goDetail(comment)">
									<text class="color-blue" v-if="comment.type == 7 || comment.type == 8">{{comment.type == 7 ? '话题帖' : '投票帖'}}</text>
									<text>{{comment.title}}</text>
								</view>
								<view class="comment-action flex-r">
									<button type="default" plain @tap.stop="deleteComment(comment, index)">删除</button>
								</view>
							</view>
						</template>
					</view>
					<view class="list-end flex-r pb20" v-if="tabData[tabCur].isEnd && comments[tabCur][0]">
						<text>已经到底啦～</text>
					</view>
				</view>
			</keep-alive>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				comments: [[],[]],
				
				tabList: [
					// {name: '方案'},{name: '讨论帖'},{name: '晒单'}
					{name: '方案'},{name: '晒单'}
				],
				tabCur: 0,
				tabData: [
					{pageNo: 1,	isEnd: false},
					{pageNo: 1,	isEnd: false},
				],
				
				navBorder: false
			}
		},
		onLoad() {
			this.getUserComments()
		},
		onReachBottom() {
			if(!this.tabData[this.tabCur].isEnd){
				this.getUserComments()
			}
		},
		onPageScroll(e) {
			if(e.scrollTop > 10){
				this.navBorder = true
			}else{
				this.navBorder = false
			}
		},
		methods: {
			//获取记录
			getUserComments() {
				this.$http.get({
					url: '/api/chtyUserComment/me/record',
					data: {
						pageNo: this.tabData[this.tabCur].pageNo,
						pageSize: 10,
						type: this.tabCur + 1==1?this.tabCur + 1:3
					},
				}).then(data => {
					console.log(data);
					if(data.success && data.result && data.result.total){
						if(data.result.current  == 1){
							this.$set(this.comments, this.tabCur, data.result.records)
						}else{
							this.comments[this.tabCur].push(...data.result.records)
						}
						this.tabData[this.tabCur].isEnd = data.result.current >= data.result.pages
						this.tabData[this.tabCur].pageNo = data.result.current + 1
					}
				}).catch((err) => {
					console.log(err);
				})
			},
			//删除留言
			deleteComment(comment, index) {
				this.$http.post({
					url: '/api/chtyUserComment/deleteCommentMsg',
					data: {
						msgId: comment.id
					}
				}).then(data => {
					console.log(data);
					if(data.success){
						this.comments[this.tabCur].splice(index, 1)
						uni.showToast({
							title: data.message || '删除成功',
							icon: 'success'
						})
					}else{
						uni.showToast({
							title: data.message || '删除失败，请稍后重试',
							icon: 'none'
						})
					}
				}).catch((err) => {
					console.log(err);
				})
			},
			//跳转详情页面
			goDetail(comment){
				if(comment.type == 3){
					this.$CHS.goShareDetail(comment.msgId)
				}else if(comment.type == 7 || comment.type == 8){
					this.$CHS.goDiscussDetail(comment.msgId)
				}else if(comment.type == 1){
					this.$CHS.goArticleDetail(comment.msgId)
				}else if(comment.type == 99){
					this.$CHS.goArticleDetail(comment.msgId, {type: 'user'})
				} 
			},
			//选项卡切换
			tabChange(index) {
				this.tabCur = index;
				if(!this.comments[index][0]){
					this.getUserComments()
				}
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
			},
			//触摸滑动开始
			touchStart(e) {
				if (e.changedTouches.length == 1) {
					this.startX = e.changedTouches[0].clientX
					this.startY = e.changedTouches[0].clientY
				}
			},
			//触摸滑动结束
			touchEnd(e) {
				if (e.changedTouches.length == 1) {
					var endX = e.changedTouches[0].clientX;
					var endY = e.changedTouches[0].clientY;
					var xTowards = endX - this.startX;
					var yTowards = endY - this.startY;
					if (xTowards > 50 && Math.abs(yTowards) < 50) {//向右
						console.log('向右')
						if(this.tabCur > 0){
							this.tabChange(this.tabCur - 1)
						}
					}
					if (xTowards < -50 && Math.abs(yTowards) < 50) {//向左
						console.log('向左')
						let maxlen = this.tabData.length - 1;
						if(this.tabCur < maxlen){
							this.tabChange(this.tabCur + 1)
						}
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.head{
		max-width: $max-width;
		margin: $pc-margin;
		width: 100%;
		position: fixed;
		background-color: #FFFFFF;
		z-index: 5;
		.tab-list{
			flex: 2;
			height: 88rpx;
			image{
				width: 1rpx;
				height: 32rpx;
			}
			.text-regular{
				flex: 2;
				text-align: center;
			}
			&.active{
				color: #0055FF;
				font-weight: 500;
				background-image: url("~@/public/images/public/zjdl_hk.png");
				background-repeat: no-repeat;
				background-position: bottom center;
				background-size: 60rpx 8rpx;
			}
		}
	}
	.body{
		margin-top: 88rpx;
		padding: 20rpx;
		.comments-list{
			.comments-list-item{
				background: #FFFFFF;
				border-radius: 10rpx;
				margin-bottom: 20rpx;
				padding: 30rpx 20rpx;
				.comment-content{
					font-size: 28rpx;
					line-height: 42rpx;
					.reply-name{
						color: #1F4799;
					}
				}
				.comment-time{
					margin-top: 20rpx;
					font-size: 24rpx;
					color: #999999;
				}
				.comment-title{
					margin-top: 20rpx;
					background: #F2F5FA;
					border-radius: 8rpx;
					padding: 20rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					color: #666;
					.color-blue{
						margin-right: 18rpx;
					}
				}
				.comment-action{
					margin-top: 20rpx;
					justify-content: flex-end;
					button{
						padding: 0;
						margin: 0;
						width: 120rpx;
						height: 60rpx;
						box-sizing: border-box;
						background: #FFFFFF;
						border: 1rpx solid #999;
						border-radius: 30rpx;
						font-size: 24rpx;
						font-weight: 500;
						line-height: 58rpx;
						color: #666;
					}
				}
			}
		}
	}
</style>
